#include "threadpool.h"
#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <unistd.h>
using namespace std;

void task1(void *i)
{
    printf("MyTask1::Run: %d\n", *((int *)i));
    sleep(1);
}

void task2(void *str)
{
    printf("MyTask2::Run: %s\n", str);
    sleep(1);
}

int main()
{
    //消费者,5个线程
    ThreadPool threadPool(5);
    char *str = "hello world!";
    int a[10] = {1,2,3,4,5,6,7,8,9,10};
    Task *task;
    //生产者，10个任务。
    for(int i = 0; i < 10; i++)
    {
        task = new Task;
        task->function = std::move(task1);
        task->arg = &a[i];
        threadPool.addTask(task);
    }

    for(int i = 0; i < 10; i++)
    {
        task = new Task;
        task->function = std::move(task2);
        task->arg = str;
        threadPool.addTask(task);
    }

    while(1)
    {
        printf("There are still %d tasks needed to complete...", threadPool.getTaskSize());
        if(threadPool.getTaskSize() == 0)
        {
            if(threadPool.stopAll() == -1){
                printf("Thread Pool clear.\n");
                exit(0);
            }
        }
        sleep(1);
        printf("1 sec later...\n");
    }
    return 0;
};
